<template>
  <!-- <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
<div class="search-btn search-btns" @click.stop="queryPageList()">
  <i class="el-icon-circle-check"></i>
  <p>查询</p>
</div>
<div class="select-box-content">
  <div class="select-box-content-name">投料机名称</div>
  <el-input v-model="feedMachineName" clearable @change="queryPageList" />
</div>
</el-collapse-item>
</el-collapse>
</template>
<template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="161" @add="add" @del="del" />
    </template>
<template v-slot:content>
      <Table :propList="propList" :listData="tableData" :showSelectColumn="true"
        @handleSelectionChange="handleSelectionChange">

        <template v-slot:status="{ row }">
          <div v-if="row.status !== undefined">{{ row.status ? '在线' : '离线' }}</div>
        </template>
<template v-slot:operation="{ row }">
          <div class="table-btns-box" v-show="row.feedMachineId">
            <div class="table-btns edit-btns" @click="edit(row)" v-if="judgePermission(161, 'edit')">
              <p>修改</p>
            </div>
            <div class="table-btns del-btns" @click="delOne(row)">
              <p>删除</p>
            </div>
          </div>
        </template>
<template v-slot:footer>
          <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
            @changeCurrentPage="changeCurrentPage" />
        </template>
</Table>
<AddEditDialog ref="addEditDialog" :feedMachineList="feedMachineList" :now-chance-stau="nowChanceStau"
  :title-text="titleText" :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog"
  @cancelDialog="cancel" />
</template>
</BaseMainSlot> -->


  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" @search="search">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <Table :propList="propList" v-loading="loading" :listData="tableData" :showSelectColumn="true" :showFooter="false"
        @handleSelectionChange="handleSelectionChange">
        <template v-slot:status="{ row }">
          <div v-if="row.status !== undefined">{{ row.status ? '在线' : '离线' }}</div>
        </template>
        <template v-slot:operation="{ row }">
          <el-button type="primary" plain v-show="row.feedMachineId" @click="edit(row)">修改</el-button>
          <el-button type="danger" plain @click="delOne(row)">删除</el-button>
          <!-- <div class="table-btns-box" v-show="row.feedMachineId">
            <div class="table-btns edit-btns" @click="edit(row)">
              <p>修改</p>
            </div>
            <div class="table-btns del-btns" @click="delOne(row)">
              <p>删除</p>
            </div>
          </div> -->
        </template>
      </Table>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="changeCurrentPage" :page="currentPage" :size="pageSize" :total="totalSize">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEditDialog ref="addEditDialog" :feedMachineList="feedMachineList" :now-chance-stau="nowChanceStau"
        :title-text="titleText" :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog"
        @cancelDialog="cancel" />
    </template>
  </MainSlot>
</template>

<script>

import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import BtnSlot from '@/components-production-management/BtnSlot';
import Table from "@/components/table/index.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import FeedingEquipmentApi from "@/api/feeding-manegement/feeding-equipment";
export default {
  name: "AreaManage",
  components: { SearchForm, BtnSlot, Pagination, MainSlot, Table, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      loading: false,
      btnList: ['add', 'delete'],
      // 控制弹出框显示隐藏
      showAddEditDialog: false,
      // 查询条件：
      feedMachineName: "",
      propList: [
        { prop: "feedMachineName", label: "投料机名称", minWidth: "80" },
        { prop: "deviceId", label: "投料机序列号", minWidth: "80" },
        { prop: "type", label: "投料机类型", minWidth: "80" },
        // { label: "关联设备", minWidth: "80", slotName: "rtuName" },
        { label: "设备状态", minWidth: "80", slotName: "status" },
        { label: "操作", minWidth: "80", slotName: "operation" },
      ],
      checkedIdList: [],
      feedMachineList: [],
      rtuObj: [],
      formItemList: [
        {
          label: '投料机名称',
          type: 'input',
          param: 'feedMachineName',
          multiple: false,
          isShow: true
        }],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.queryByCompanyMark()
  },
  mounted() {
    this.queryPageList();
  },
  methods: {

    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.add();
      } else if (flag == 'delete') {
        this.delmessage();
      }
    },
    //获取关联设备
    queryByCompanyMark() {
      let companyMark = sessionStorage.getItem('companyMark')
      if (companyMark) {
        FeedingEquipmentApi.queryByCompanyMark({
          companyMark: sessionStorage.getItem('companyMark')
        }).then(res => {
          if (res.data && res.data.length > 0) {
            this.feedMachineList = res.data
            res.data.forEach(item => {
              this.rtuObj[item.rtuId] = item.rtuName
            })
          } else {
            this.feedMachineList = []
          }
          console.log('关联设备', res.data)
        })
      } else {
        this.$message.error('未获取到关联设备！');
      }
    },
    search(data) {
      this.currentPage = 1;
      this.feedMachineName = data.feedMachineName;
      this.queryPageList();
    },
    queryPageList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {}
      };
      this.loading = true;
      if (this.feedMachineName) params.entity.feedMachineName = this.feedMachineName;
      FeedingEquipmentApi.queryPageByCondition(params).then(res => {
        this.loading = false;
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.resetActionBar();
        }
      }).catch(err => {
        this.loading = false;
      });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    handleEdit(row) {
      if (row.feedMachineId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.addActive = true;
        this.editActive = true;
      } else {
        this.editObject = {};
        this.addActive = true;
        this.editActive = false;
      }
    },
    selectAll(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.feedMachineId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.feedMachineId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.feedMachineId) {
          return item.feedMachineId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          FeedingEquipmentApi.delById({ id: data.feedMachineId }).then(res => {
            if (res.success === true) {
              this.$message.success("删除成功!");
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage() {
      if (!this.checkedIdList.length) return;
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          FeedingEquipmentApi.delListById({ idList: this.checkedIdList }).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });

    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.feedMachineId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
